package com.yc.bbs.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.bbs.dao.TblUserMapper;
import com.yc.bbs.entity.Result;
import com.yc.bbs.entity.TblUser;
import com.yc.bbs.entity.UserEx;
import com.yc.bbs.service.BizException;
import com.yc.bbs.service.ITblUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.UUID;

/**
 * <p>
 * BBS论坛-用户表-6 服务实现类
 * </p>
 *
 * @author k
 * @since 2024-11-25
 */
@Service
public class TblUserServiceImpl extends ServiceImpl<TblUserMapper, TblUser> implements ITblUserService {
    @Autowired
    TblUserMapper userMapper;
    @Override
    public TblUser login(TblUser user) throws BizException {
        LambdaQueryWrapper<TblUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TblUser::getUname,user.getUname());
        lqw.eq(TblUser::getUpass,user.getUpass());
        TblUser tblUser = userMapper.selectOne(lqw);
        if(tblUser==null){
            throw new BizException("用户名或密码错误",null);
        }
        System.out.println("user = " + tblUser);
        return tblUser;
    }

    @Override
    public void reg(UserEx user, Errors errors) throws BizException {
        if(!user.getUpass().equals(user.getUpass1())){
            errors.rejectValue("upass1","0000",
                    "两次密码不一致");
        }
        //同名验证
        LambdaQueryWrapper<TblUser> lqw=new LambdaQueryWrapper<>();
        lqw.eq(TblUser::getUname,user.getUname());
        if(count(lqw)>0){
            errors.rejectValue("upass1","0001",
                    "该用户名已经注册");
        }
        if(errors.hasErrors()){
            throw new BizException("字段验证错误",errors.getAllErrors());
        }
        user.setRegtime(new Timestamp(System.currentTimeMillis()));
//        user.setRegtime(new Timestamp(System.currentTimeMillis()).toLocalDateTime());
        this.save(user);
    }
    public int updateHead(TblUser myinfo,String head){
        LambdaQueryWrapper<TblUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(TblUser::getId,myinfo.getId());
        myinfo.setHead(head);
        int update = userMapper.update(myinfo, lqw);
        return update;
    }
}
